package com.xt.wikiperfumapi.Controller.Ingredient;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.xt.wikiperfumapi.model.FragranceNoteInfo;
import com.xt.wikiperfumapi.model.IngredientInfo;
import com.xt.wikiperfumapi.model.vo.RestBean;
import com.xt.wikiperfumapi.model.wrapper.IngredientWrapper;
import com.xt.wikiperfumapi.service.impl.IngredientInfoServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/ingredient")
@Slf4j
public class IngredientController {

    @Resource
    IngredientWrapper ingredientWrapper;

    @Resource
    IngredientInfoServiceImpl ingredientInfoService;
    @Autowired
    private IngredientInfoServiceImpl ingredientInfoServiceImpl;

    @RequestMapping("/get/{id}")
    public RestBean getById(@PathVariable long id){
        try {
            IngredientInfo byId = ingredientInfoService.getById(id);
            return RestBean.success(ingredientWrapper.ingredientToVo(byId));
        }catch (Exception e){
            log.info(e.getMessage());
            return RestBean.failure(400,"不存在该记录");
        }
    }

    @RequestMapping("/fragranceNoteId/{id}")
    public RestBean queryByFragranceNoteId(@PathVariable long id){
        List<IngredientInfo> ingredientInfos = ingredientInfoService.list(new QueryWrapper<IngredientInfo>()
                .eq("fragrance_note_id", id)
                .eq("is_deleted", 0)
        );
        return RestBean.success(ingredientInfos);
    }


    @RequestMapping("/search/{page}/{item}")
    public RestBean searchByLikeName(String  name,@PathVariable int page,@PathVariable int item){
        PageMethod.startPage(page,item);
        List<IngredientInfo> ingredientInfos = ingredientInfoService.list(new QueryWrapper<IngredientInfo>()
                .like("name", name)
        );
        return RestBean.success(new PageInfo<>(ingredientInfos));
    }


    @RequestMapping("/list/{page}/{item}")
    public RestBean list(@PathVariable int page ,@PathVariable int item){
        PageMethod.startPage(page,item);
        List<IngredientInfo> fragranceNoteInfos = ingredientInfoService.list(
                new QueryWrapper<IngredientInfo>()
                .eq("is_deleted", 0)
        );
        return RestBean.success(new PageInfo<>(fragranceNoteInfos));
    }

    @RequestMapping("/update")
    public RestBean update(@RequestBody IngredientInfo info){
        try {
            IngredientInfo byId = ingredientInfoService.getById(info.getIngredientId());
            byId.setName(info.getName());
            byId.setImg(info.getImg());
            byId.setFragranceNoteId(info.getFragranceNoteId());
            byId.setUpdateTime(new Date());
            return ingredientInfoService.updateById(byId)?
                    RestBean.success(byId):RestBean.failure(500,"server error");
        }catch (Exception e){
            return RestBean.failure(400,"不存在该原料信息");
        }
    }

    @RequestMapping("insert")
    public RestBean insert(@RequestBody IngredientInfo info){
        info.setInputTime(new Date());
        info.setIngredientId((long)ingredientInfoServiceImpl.list().size()+3000);
        return ingredientInfoService.save(info)?
                RestBean.success(info):RestBean.failure(500,"server error");
    }

    @RequestMapping("/del/{id}")
    public RestBean del(@PathVariable long id){
        try {
            return ingredientInfoService.removeById(id)?
                    RestBean.success():RestBean.failure(500,"server error");
        }catch (Exception e){
            return RestBean.failure(400,"不存在该原料信息");
        }
    }

    @RequestMapping("/getid/{id}")
    public RestBean getByid(@PathVariable long id){
        try {
            return RestBean.success(ingredientInfoService.getById(id));
        }catch (Exception e){
            return RestBean.failure(400,"不存在该原料信息");
        }
    }


}
